﻿@using Seal.Model
@using Seal.Helpers
<div class="text-center" style="width:250px">
    @{
        Report report = Model;
        ReportView view = report.CurrentView;
        ReportView modelView = report.CurrentModelView;
        ReportModel reportModel = modelView.Model;
        ResultPage page = report.CurrentPage;
        ResultTable table = page.DataTable;

        int trendIndex = view.GetNumericValue("kpi_trend_index");
        string buttonSize = view.GetValue("kpi_button_size");
        string goalSuffix = "_GOAL";
        string newWidgetColor = "", newWidgetIcon = "";
    }

    @functions {
        public string getValueFromRange(string definition, int percentage)
        {
            var result = "";
            foreach (var def in definition.Replace("\r\n", "\n").Split('\n'))
            {
                if (!string.IsNullOrEmpty(def))
                {
                    var defs = def.Split('|');
                    if (defs.Length == 1) result = defs[0];
                    else if (defs.Length > 1)
                    {
                        int threshold;
                        if (int.TryParse(defs[1], out threshold))
                        {
                            if (percentage < threshold) result = defs[0];
                        }
                    }
                }

                if (!string.IsNullOrEmpty(result)) break;
            }
            return result;
        }
    }

    @{
        for (int col = 0; col < table.ColumnCount && table.RowCount > 1; col++)
        {
            string headerValue = "", lastValue = "", trendColor = "", trendIcon = "", goalValue = "", goalColor = "", goalIcon = "";
            int? trendPercentage = null, goalPercentage = null;
            var lastCell = table[table.BodyEndRow - 1, col];
            if (table.BodyStartRow == table.BodyEndRow && table.BodyEndRow < table.RowCount)
            {
                lastCell = table[table.BodyEndRow, col]; //Case only one value
            }
            if (lastCell.Element != null && lastCell.Element.IsNumeric && !lastCell.IsTotal && lastCell.Element.PivotPosition == PivotPosition.Data && lastCell.Value != null && !lastCell.Element.DisplayNameEl.EndsWith(goalSuffix))
            {
                if (table.BodyStartRow == 0) { headerValue = lastCell.Element.DisplayNameElTranslated; }
                else if (table.BodyStartRow == 1 || (table.BodyStartRow == 2 && !reportModel.ShowFirstLine)) { headerValue = table[0, col].HTMLValue; }
                else { headerValue = table[1, col].HTMLValue; }

                lastValue = lastCell.HTMLValue;
                var valueEnd = lastCell.DoubleValue;

                double? valueStart = null;
                int rowStart = table.BodyEndRow - 2;
                while (rowStart >= table.BodyStartRow && (trendIndex < 0 || trendIndex + 1 >= table.BodyEndRow - rowStart || valueStart == null))
                {
                    if (table[rowStart, col].Value != null) { valueStart = table[rowStart, col].DoubleValue; }
                    --rowStart;
                }

                int val = 0;
                if (valueStart != null && valueStart != 0)
                {
                    trendPercentage = Convert.ToInt32(100 * (valueEnd - valueStart) / valueStart);
                    val = trendPercentage.Value;
                }
                trendColor = getValueFromRange(view.GetValueOrDefault("kpi_trend_color"), val);
                newWidgetColor = trendColor.Replace("btn-", "");
                trendIcon = getValueFromRange(view.GetValueOrDefault("kpi_trend_icon"), val);
                newWidgetIcon = trendIcon;

                //Check if a Goal is defined
                for (int colGoal = 0; colGoal < table.ColumnCount; colGoal++)
                {
                    var goalCell = table[table.BodyEndRow - 1, colGoal];
                    if (goalCell.Element != null && goalCell.Element.DisplayNameEl == lastCell.Element.DisplayNameEl + goalSuffix && goalCell.Element.IsNumeric && goalCell.Value != null)
                    {
                        goalValue = string.Format("{0}: {1}", report.Translate("Goal"), goalCell.HTMLValue);
                        val = 0;
                        if (goalCell.DoubleValue != 0)
                        {
                            goalPercentage = Convert.ToInt32(100 * (valueEnd - goalCell.DoubleValue) / goalCell.DoubleValue);
                            val = goalPercentage.Value;
                        }
                        goalColor = getValueFromRange(view.GetValueOrDefault("kpi_goal_color"), val);
                        goalIcon = getValueFromRange(view.GetValueOrDefault("kpi_goal_icon"), val);
                    }
                }

                <span class="text-center btn @trendColor @buttonSize" title="@Raw(headerValue)" style="margin-top:5px;">
                    <span class="@trendIcon" aria-hidden="true"></span> @Raw(headerValue) <strong>@Raw(lastValue)</strong>
                    @if (trendPercentage != null)
                    {
                        <span class="badge">@Raw(string.Format("{0:+0;-0;0} %", trendPercentage.Value))</span>
                    }
                </span>

                if (!string.IsNullOrEmpty(goalValue))
                {
                    <span class="text-center label @goalColor" title="@Raw(headerValue)" style="margin-top:0px;font-size: 12px;">
                        <span class="glyphicon @goalIcon" aria-hidden="true"></span> @Raw(goalValue) @Raw(goalPercentage != null ? string.Format("({0:+0;-0;0} %)", goalPercentage.Value) : "")
                    </span>
                }
            }
        }
    }

    @if (!string.IsNullOrEmpty(newWidgetColor))
    {
        <input type="hidden" id="new-widget-color" name="new-widget-color" value="@newWidgetColor" />
    }
    @if (!string.IsNullOrEmpty(newWidgetIcon))
    {
        <input type="hidden" id="new-widget-icon" name="new-widget-icon" value="@newWidgetIcon" />
    }
</div>